package com.whc.gulimall.order;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.whc.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000117695495";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCKLU1hSU4nOwNKjk1seX+OajXOmeidfyeaoiW2pc1zY8otDbZjiKcOBAS1YfpjZ+GIJtEMM85v0w4MGN7y426FJPxTgBxTqAi3t/qRwnG3f17Yt8vAhr2iC9YVNWPy6dBfAi1nii/LghhF7sjNC5MKDtAAPlXSgpJjxF7Z1fAha+YTBewwN+zydvbai7WPoam/FyUW7wKUZaZTlHt6lcMLhnAPowkd23pyQV86tDbw5TEeeb5Vml2P5tSe37iTZt6+mej8fzXdxwmvzF56UnReVD8J/tjZssq4AoVCWpQLT6CKnkaGMqzh8Fc8MgUImeQ9SfSUCCBsikWXMkZvOuizAgMBAAECggEAKgUg1QcP50pA8/lWfjhsnAicmFmhrdOvXY0V3veJBSBBqXXUE2zJMK/0JpzHIlfC9D6psAZJLySFzxP5KPAU+hSHT3VUJ/Sfwg8G+jChmGX5l/o80i2nmZHz5rs3y/Vi0qG84RHa+w5zB0FYe1KbwqFCi/Bo9P2UOxbKqYAv6FmM4w9+8wigzHbDZvVUmQUAO4bvgmBXP+IW3/fPNN/nLe5wFUHnQObvWNh3jlKUV2XF5s/0GldfQdtOcv1FPLack8RvTzFAcGsk/A0syBXIDEc8pJkr4IoOVzwVN/mSd/UkU0tyBb+xMwXgMuQg0pECocA40lCKdP+nw8qZ8CXesQKBgQDnTBoDoHeFlV2CLHXOm9HzOG5uawHmL5Ebm6LOgv49fBnnMvXbuEbsFIUP+CCKv253NZW4YkYz0pcaRVPFQ3nC13dCeEE6rx2VftjYJXwu66Heneh+411Ffc2t/ZP762nYUhDga4dAdf7qCXK8SBk4DPXXhEtFKZcWzeHEiieW9QKBgQCY7zLJayYcvwJjUTUEsmJi5gfbDq4fwaF16LHYyQ2eGKOCxwUiDqSpFiPi3Tp3/dnsiXrwkpzsCs+IBPqlNw9wmP/TqJYNCn7x2dWwI22VSqE0NkTyYlxrcZ02k9bTFshdEArTZkDFwxHHuDpRHjImGnSamegBcVGb7JuTqaGoBwKBgHyjsdEKg93kY4NmWgWASl76peeymk9kipmKAO2wATk4yuyftYU1hGJrnFjCqSabifeLVKcLrjD25p4thbrjZZXtbkLPcrcg7pxEwq2XZZgTOiMwjLzz4Ks148RCxaAXd+4uKxIEX9oIu9l6Re3YHF7r/P6qN7WJd/W99E9o83SRAoGAIjNb6C9OFp+gqRVABgzec799gj+GMRPcR8ICA7jykqhv6I8ZVsRvjmClJdbTfIIIeh1EoQgoj6DbSE/UvZ3SlhuhGnRZGhF+0hck8n75wqkt478/K/VTNbPytIHbSNmPdUBhK2VylsdI8qEAaaOXE25XJTvF5oyw8222XQ9SV4ECgYB/zvhISLB7TmTyn9ljsbXo5w4aNjmP2Dub8uuVtwqll0YWn/9ieCKv6PEN6JqwFT5Mr/NREV4OWmyZIw8fUcrGV/vN/16q/hp+G4APCto07T4dE1kn8wCEnGQG+UPO8OcT0fgnYY4GrG7GxtErW/9vlidWZpTr0dxGg1/P0CGW5Q==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhBaKRIq+W/bJe/acZago8BdjvjMqHeVR8dT4aNvE+CrzOgej28had87ZZfo0fqiC+CeygOUZHjudzZa5quuQUw1ykqHjz5sSFr+gEW6dhmx7oFA+kc2gFNbqtqHi9/ggDp8Oga9CC3ZLybOaJlFYoq0YufSnwuzp6DxDBzsinIyzAqrWBCk8YWAWj3/xOB2Bv4wq88ZdZ05dJLFbglLcOCMbNAVsUhNgOfdiaMbp6/qMPzL4VzybmR3NYvvZEy0yJQ1gK15Lx71VyULJQol13t02Si57NRAGrUSYWOTPafB8IwOuQoSbGrEFFPVPn6WKy95Pi1N3oo7rf6ItPMdHAQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://iiwmrg.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
